package model;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SaleRecord {
    private String id;
    private String computerId;
    private String customerId;
    private int quantity;
    private double totalAmount;
    private LocalDateTime saleTime;

    public SaleRecord(String id, String computerId, String customerId, int quantity, double totalAmount) {
        this.id = id;
        this.computerId = computerId;
        this.customerId = customerId;
        this.quantity = quantity;
        this.totalAmount = totalAmount;
        this.saleTime = LocalDateTime.now();
    }

    public SaleRecord(String id, String computerId, String customerId, int quantity, double totalAmount, String saleTime) {
        this.id = id;
        this.computerId = computerId;
        this.customerId = customerId;
        this.quantity = quantity;
        this.totalAmount = totalAmount;
        this.saleTime = LocalDateTime.parse(saleTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getComputerId() {
        return computerId;
    }

    public void setComputerId(String computerId) {
        this.computerId = computerId;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public LocalDateTime getSaleTime() {
        return saleTime;
    }

    public void setSaleTime(LocalDateTime saleTime) {
        this.saleTime = saleTime;
    }

    @Override
    public String toString() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return id + "," + computerId + "," + customerId + "," + quantity + "," +
                totalAmount + "," + saleTime.format(formatter);
    }

    public static SaleRecord fromString(String data) {
        String[] parts = data.split(",");
        if (parts.length == 6) {
            return new SaleRecord(parts[0], parts[1], parts[2], Integer.parseInt(parts[3]),
                    Double.parseDouble(parts[4]), parts[5]);
        }
        return null;
    }
}